import http from 'utils/http';
import { Article } from 'pages/index/Home/ArticleItem';
import Taro from '@tarojs/taro';

let path = 'article/v1/album/list'
export function articleListApi() {
  const api = (_id: string) => {
    var key = `${path}:${_id}`
    var timekey = `${key}:time`
    
    var time = Taro.getStorageSync(timekey) || 0
    var curtime = Date.now();
    var dis = curtime - parseInt(time);
    var arr = Taro.getStorageSync(key)

    if (!arr || dis > 3600000) {
      return http.get<Article[]>('article/v1/album/list', {album_id: _id}).then(res => {
        Taro.setStorageSync(key, JSON.stringify(res.data))
        Taro.setStorageSync(timekey, Date.now())
        return res
      })
    }

    return http.local<Article[]>(JSON.parse(arr));
  }
  return {
    api,
    data: []
  }
}
